<!--********************************************************************
* Copyright© 2000 - 2025 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!--********************************************************************
* 该示例需要引入
* UGCWasm (https://iclient.supermap.io/web/libs/ugcwasm/1.0.1/UGCWasmAll.js)
*********************************************************************-->
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title data-i18n="resources.title_ugcGeodesicArea"></title>
    <style>
      body {
        margin: 0;
        overflow: hidden;
        background: #fff;
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
      }
      #toolbar {
        position: absolute;
        top: 50px;
        right: 10px;
        width: 280px;
        text-align: center;
        z-index: 10000;
        border-radius: 4px;
      }
      .ol-popup {
        position: absolute;
        background-color: white;
        -webkit-filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
        filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
        padding: 15px;
        border-radius: 10px;
        border: 1px solid #cccccc;
        bottom: 12px;
        left: -50px;
        min-width: 280px;
      }

      .ol-popup:after,
      .ol-popup:before {
        top: 100%;
        border: solid transparent;
        content: ' ';
        height: 0;
        width: 0;
        position: absolute;
        pointer-events: none;
      }

      .ol-popup:after {
        border-top-color: white;
        border-width: 10px;
        left: 48px;
        margin-left: -10px;
      }

      .ol-popup:before {
        border-top-color: #cccccc;
        border-width: 11px;
        left: 48px;
        margin-left: -11px;
      }
    </style>
  </head>

  <body>
    <div id="toolbar" class="panel panel-primary">
      <div class="panel-heading">
        <h5 class="panel-title text-center">计算面积</h5>
      </div>
      <div class="panel-body content">
        <input type="button" class="btn btn-default" value="计算" onclick="search()" />
      </div>
    </div>
    <div id="map" style="width: 100%; height: 100%"></div>
    <div id="popup" class="ol-popup">
      <div id="popup-content"></div>
    </div>
    <script type="text/javascript" include="bootstrap-css" src="../js/include-web.js"></script>
    <script type="text/javascript" include="ugcwasm" src="../../dist/ol/include-ol.js"></script>
    <script>
      var container = document.getElementById('popup');
      var content = document.getElementById('popup-content');
      var overlay = new ol.Overlay({
        element: container,
        autoPan: true,
        autoPanAnimation: {
          duration: 250
        }
      });
      var map, popup, polygon, polygonLayer;
      var map, bufferLayer;
      var host = window.isLocal ? window.server : 'https://iserver.supermap.io';
      var url = host + '/iserver/services/map-world/rest/maps/World';
      ol.supermap
        .initMap(url, {
          viewOptions: {
            center: [120, 30],
            zoom: 6
          },
          mapOptions: {
            overlays: [overlay]
          }
        })
        .then((res) => {
          map = res.map;
          addFeatures();
        });

      function addFeatures() {
        polygon = {
          type: 'Feature',
          properties: {},
          geometry: {
            coordinates: [
              [
                [121.74162512536924, 32.60736281118376],
                [120.59633803460247, 32.42386901254892],
                [119.76027845834045, 31.47151252648368],
                [120.36728061644811, 29.925467312116822],
                [121.6843607708318, 29.93539273511712],
                [122.37153302529236, 30.627700930765172],
                [123.15032824701461, 31.66667145093632],
                [122.77238350706119, 32.597714586070424],
                [121.74162512536924, 32.60736281118376]
              ]
            ],
            type: 'Polygon'
          }
        };
        var polygonSource = new ol.source.Vector({
          features: [new ol.format.GeoJSON().readFeature(polygon)],
          wrapX: false
        });
        polygonLayer = new ol.layer.Vector({
          source: polygonSource,
          style: new ol.style.Style({
            stroke: new ol.style.Stroke({
              color: 'blue',
              width: 3
            }),
            fill: new ol.style.Fill({
              color: 'rgba(0, 0, 255, 0.1)'
            })
          })
        });
        map.addLayer(polygonLayer);
      }

      function search() {
        var geometryAnalysis = new ol.supermap.GeometryAnalysis();
        var area;
        area = geometryAnalysis.computeGeodesicArea(polygon);
        var innerHTML = '面积为: ' + area + '平方米';
        content.innerHTML = innerHTML;
        overlay.setPosition([121, 31]);
      }
    </script>
  </body>
</html>
